package com.tuhao.www.rest.controller.user;

import com.github.pagehelper.PageInfo;
import com.tuhao.www.common.JsonResult;
import com.tuhao.www.domain.user.User;
import com.tuhao.www.service.user.UserModelService;
import com.tuhao.www.util.controller.TokenController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author tuhao on 2017/3/28.
 */
@Controller
@RequestMapping(value = "/mybatis/user")
public class UserRestController extends TokenController {
    @Resource
    private UserModelService userModelService;

    @RequestMapping(value = "select", method = RequestMethod.GET)
    @ResponseBody
    public JsonResult select(JsonResult json, Long id){
        if (id==null) id = 1L;
        User user = userModelService.selectByPrimaryKey(id);
        success(json,user);
        return json;
    }

    @RequestMapping(value = "list", method = RequestMethod.GET)
    @ResponseBody
    public JsonResult list(JsonResult json, String aoData) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        //Map<String, Object> resultMap = getParams(aoData);
        //记录操作的次数
        //String sEcho = resultMap.get("sEcho").toString();
        // 起始索引
        int iDisplayStart = 0; //Integer.valueOf(resultMap.get("iDisplayStart").toString());
        // 每页显示的行数
        int iDisplayLength =10; //Integer.valueOf(resultMap.get("iDisplayLength").toString());

        Map<String, Object> param = new HashMap<String, Object>();
        //搜索条件为佣金等级或称号
        //String serchName = String.valueOf(resultMap.get("searchName"));
        //param.put("searchName", serchName);

        PageInfo pageInfo = userModelService.pageFindModel("UserDao.findAllBySearchName", getPage(iDisplayStart, iDisplayLength), param);

        success(json, generatorPage("", pageInfo));
        return json;
    }

    @RequestMapping(value = "sss", method = RequestMethod.GET)
    @ResponseBody
    public JsonResult sss(JsonResult json, Long id){
        if (id==null) id = 1L;
        User user = new User();
        user.setUserName("444");
        userModelService.save(user);
        success(json,null);
        return json;
    }
}
